package com.tosit.driverschool.controller;

import com.tosit.driverschool.common.entity.ProblemSet;
import com.tosit.driverschool.common.entity.SetType;
import com.tosit.driverschool.common.entity.Subject;
import com.tosit.driverschool.service.ProblemSetService;
import com.tosit.driverschool.service.SetTypeService;
import com.tosit.driverschool.service.VehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Controller
@RequestMapping("/p")
public class ProblemSetController {

    @Autowired
    ProblemSetService problemSetService;
    @Autowired
    VehicleService vehicleService;
    @Autowired
    SetTypeService setTypeService;

    @GetMapping("/list")
    public String manage(ModelMap modelMap) {
        modelMap.put("content", "set-list");
        return "index";
    }

    @GetMapping("/{subject}")
    public String subject(@PathVariable(name = "subject")String subject, ModelMap modelMap){
        String sub =c(subject);
       first(sub, modelMap);
        modelMap.put("content", "set-list");
       return "index";
    }

    public void first(String sub,ModelMap modelMap){
        Set<String> vehicleTypes = vehicleService.getAllType(sub);
        Object[] vtypes = vehicleTypes.toArray();
        if(vtypes.length > 0){
            String firstVehicleType = (String)(vtypes[0]);
            //某车型下的套题大类
            Set<SetType> psetTypes = problemSetService.findAllType(sub, firstVehicleType);
            modelMap.addAttribute("psetTypes", psetTypes);
        }
    }

    @ResponseBody
    @PostMapping("/manage/delete/{id}")
    public Object delete(@PathVariable(name = "id") Integer id){
          Integer result = problemSetService.delete(id);
          return result;
    }

    @PostMapping("/manage/add")
    public String add(SetType setType, String setName){
        Integer typeId = setTypeService.findId(setType);
        problemSetService.add(new ProblemSet(setName,typeId));
        return "redirect:/p/subject1";
    }

    @ResponseBody
    @GetMapping("/set/{setId}")
    public Object getSetQuestion(@PathVariable(name = "setId") Integer setId){
        List<Subject> subjects = problemSetService.find(setId);
        return subjects;
    }

    @ResponseBody
    @GetMapping("/type/{typeId}")
    public Object getSetList(@PathVariable(name = "typeId") Integer typeId){
        List<ProblemSet> all = problemSetService.findAll(typeId);
        return all;
    }

    @GetMapping("/{subject}/{vehicleType}")
    public Object getAllType(@PathVariable(name = "subject") String subject, @PathVariable(name = "vehicleType") String vehicleType, ModelMap modelMap) {
        String sub =c(subject);
        first(sub, modelMap);
        Set<SetType> psetTypes = problemSetService.findAllType(sub, vehicleType);
        modelMap.addAttribute("psetTypes", psetTypes);
        modelMap.addAttribute("content", "set-list");
        return "index";
    }

//    @GetMapping("/{subject}/{vehicleType}/{typeId}")
//    public Object getAllType(@PathVariable(name = "subject") String subject,
//                             @PathVariable(name = "vehicleType") String vehicleType,
//                             @PathVariable(name = "typeId") Integer typeId,
//                             ModelMap modelMap) {
//        String sub =c(subject);
//        Set<SetType> psetTypes = problemSetService.findAllType(sub, vehicleType);
//        modelMap.addAttribute("psetTypes", psetTypes);
//        List<ProblemSet> all = problemSetService.findAll(typeId);
//        modelMap.addAttribute("problemSets", all);
//        modelMap.addAttribute("content","set-list");
//        return "index";
//    }

    private String c(String subject){
        String sub;
        if(subject.equals("subject1")){
            sub="科目一";
        }
        else {
            sub="科目四";
        }
        return sub;
    }
}
